package com.pengtu.gsj.controller.system;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.pengtu.gsj.controller.base.BaseController;
import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.entity.app.Code;
import com.pengtu.gsj.interceptor.Token;
import com.pengtu.gsj.service.system.CodeService;
import com.pengtu.gsj.utils.web.SpringMvcUtils;

/**
 * 
 * 
 * CodeController
 * 
 * 2017年4月18日 下午1:56:26
 * 
 * @version 1.0.0
 *
 */
@Controller
@RequestMapping("/code/")
public class CodeController extends BaseController {

	@Autowired
	private CodeService codeService;
	
	/**
	 * 
	 * getCodes:
	 * 适用:按属性要求获取码表列表
	 * @param page 对象集
	 * @param request
	 * @param model 传递参数
	 * @return 码表列表页面
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping(value = "list", produces = "application/json; charset=utf-8")
	public Page<Code> getCodes(Page<Code> page,
			HttpServletRequest request, Model model) {
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(request);
		if (!page.isOrderBySetted()) {
			page.setOrderBy("category,codeId");
			page.setOrder("asc,asc");
		}
		page = codeService.findCodes(page, queryFilter);
		return page;
	}
	
	/**
	 * 
	 * loadEntity:
	 * 适用: 每个方法执行之前都会执行该方法
	 * @param id  code主键id
	 * @return 返回持久对象code或者新创建对象
	 * @exception 
	 * @since  1.0.0
	 */
	@ModelAttribute
	public Code loadEntity(String id,Model model){
		Code code ;
		if (StringUtils.isNotEmpty(id)) {
			code = codeService.getCode(id);
		}else {
			code = new Code();
		}
		model.addAttribute("top", SpringMvcUtils.getParameter("top"));
		model.addAttribute("left", SpringMvcUtils.getParameter("left"));
		return code;
	}
	
	/**
	 * 
	 * saveCode:
	 * 适用:保存对象
	 * @param code 对象
	 * @param request 请求
	 * @param model 传递参数
	 * @return 码表列表页面
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping(value = "save",method=RequestMethod.POST)
	@Token(remove = true)
	public String saveCode(@ModelAttribute Code code,RedirectAttributes attributes,Model model) {
			codeService.saveCode(code);
			attributes.addFlashAttribute("msg", "保存成功！");
			attributes.addAttribute("top",	SpringMvcUtils.getParameter("top"));
			attributes.addAttribute("left", SpringMvcUtils.getParameter("left"));
		return "redirect:/code/show.do";
	}
	
	
	/**
	 * 
	 * deleteCode:删除对象
	 * 适用:
	 * @param id 唯一参数标识
	 * @return 码表列表页面
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping(value = "delete")
	public String deleteCode(String id,RedirectAttributes attributes){
		codeService.deleteCode(id);
		attributes.addFlashAttribute("msg", "删除成功！");
		attributes.addAttribute("top",	SpringMvcUtils.getParameter("top"));
		attributes.addAttribute("left", SpringMvcUtils.getParameter("left"));
		return "redirect:/code/show.do";
	}
	
	/**
	 * 
	 * codeShow:展示页面
	 * 适用:
	 * @param model 传递参数
	 * @return 码表列表页面
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("show")
	public String codeShow(Model model) {
		String msg = SpringMvcUtils.getParameter("msg");
		if (StringUtils.isNotEmpty(msg)) {
			model.addAttribute("msg", msg);
		}
		return "code/codeList";
	}
	
	/**
	 * 
	 * codeInput:新增页面
	 * 适用:
	 * @param id 唯一id标识
	 * @param model 传递参数
	 * @return 新增/修改页面
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("input")
	@Token(save = true)
	public String codeInput(@ModelAttribute Code code,RedirectAttributes attributes,Model model){
		if(code!=null){
			model.addAttribute("entity", code);
		}
		attributes.addAttribute("top",	SpringMvcUtils.getParameter("top"));
		attributes.addAttribute("left", SpringMvcUtils.getParameter("left"));
		return "code/codeInput";
	}

	/**
	 * 
	 * checkId:
	 * 适用:检查codeId是否可用
	 * @return 可用/不可用
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping(value="checkId",produces = "application/json; charset=utf-8")
	public boolean checkId(){
		String category = SpringMvcUtils.getParameter("category");
		String codeId = SpringMvcUtils.getParameter("codeId");
		Integer categorya = 0;
		String oldcategory = SpringMvcUtils.getParameter("oldcategory");
		String oldcodeId = SpringMvcUtils.getParameter("oldcodeId");
		if(StringUtils.isNotEmpty(oldcategory)&&StringUtils.isNotEmpty(oldcodeId)){
			if(codeId.equals(oldcodeId)&&category.equals(oldcategory)){
				return true;
			}else {
				if(StringUtils.isNotEmpty(category)){
					categorya = Integer.parseInt(category);
				}
				Boolean exist = codeService.checkId(categorya, codeId);
				return exist;
			}
		}else{
			if(StringUtils.isNotEmpty(category)){
				categorya = Integer.parseInt(category);
			}
			Boolean exist = codeService.checkId(categorya, codeId);
			return exist;
		}
		
	}

}
